import { Scene_MenuBase } from "@/scenes/Scene_MenuBase.js";
import { Window_SkillType } from "@/windows/Window_SkillType.js";
import { Window_SkillStatus } from "@/windows/Window_SkillStatus.js";
import { Window_SkillList } from "@/windows/Window_SkillList.js";
import { Window_MenuStatus } from "@/windows/Window_MenuStatus.js";

import { Game_Action } from "@/objects/GameAction.js";
import { config } from "@/managers/ConfigManager.js";

import { SceneManager } from "@/managers/SceneManager.js";
import { SoundManager } from "@/managers/SoundManager.js";

export class Scene_Skill extends Scene_MenuBase {
    constructor() {
        super();
        /**
         * @type {Game_Action}
         */
        this._action=null;
    }

    create() {
        super.create();
        this.createHelpWindow();
        this.createSkillTypeWindow();
        this.createStatusWindow();
        this.createItemWindow();
        this.createActorWindow();

        this.attachHandler();
    }

    start() {
        super.start();
        this.refreshActor();
    }

    createSkillTypeWindow() {
        this._skillTypeWindow = new Window_SkillType(240,180);
        this._skillTypeWindow.y=this._helpWindow.height;
        this.addWindow(this._skillTypeWindow);
    }

    createStatusWindow() {
        this._statusWindow = new Window_SkillStatus(config.width-240,180);
        this._statusWindow.x=240;
        this._statusWindow.y=this._helpWindow.height;
        this.addWindow(this._statusWindow);
    }

    createItemWindow() {
        let y=this._skillTypeWindow.y+this._skillTypeWindow.height
        let h=config.height-y;
        this._itemWindow = new Window_SkillList(config.width,h);
        this._itemWindow.y=y;
        this._itemWindow.deactivate();
        this.addWindow(this._itemWindow);
    }

    createActorWindow(){
        this._actorWindow=new Window_MenuStatus(config.width-240,config.height);
        this._actorWindow.x=240;
        this._actorWindow.hide();
        this._actorWindow.deactivate();
        this.addChild(this._actorWindow);
    }

    attachHandler(){
        this._skillTypeWindow.setHandler('changed',(t,ot)=>{
            this._itemWindow.setStypeId(t);
        });
        this._skillTypeWindow.setHandler('ok',(t)=>{
            this._skillTypeWindow.deactivate();
            this._itemWindow.activate();
            this._itemWindow.select(0);
        });
        this._skillTypeWindow.setHandler('cancel',()=>{
            SceneManager.pop();
        });

        this._itemWindow.setHandler('changed',(item,index,oitm,oi)=>{
            this._helpWindow.setItem(item);
        });
        this._itemWindow.setHandler('ok',(i)=>{
            if(i){
                this.determineItem(i);
            }
        });
        this._itemWindow.setHandler('cancel',()=>{
            this._skillTypeWindow.activate();
            this._itemWindow.deactivate();
        });

        this._actorWindow.setHandler('ok',actor=>{
            let item=this._action.item();
            if(!actor||!this._action||!actor.canUse(item))return;
            
            this._action.setSubject(actor);
            this.useItem(actor,this._action,item);
            // this.checkCommonEvent();
            // this.checkGameover();
            this._actorWindow.refreshItem(actor.index());

        });
        this._actorWindow.setHandler('cancel',()=>{
            this._actorWindow.deactivate();
            this._actorWindow.close();
            this._itemWindow.activate();
        });

    }

    refreshActor() {
        let actor = this.actor();
        this._skillTypeWindow.setActor(actor);
        this._statusWindow.setActor(actor);
        this._itemWindow.setActor(actor);
    }

    user() {
        return this.actor();
    }

    commandSkill() {
        this._itemWindow.activate();
        this._itemWindow.selectLast();
    }

    onItemOk() {
        this.actor().setLastMenuSkill(this.item());
        this.determineItem();
    }

    onItemCancel() {
        this._itemWindow.deselect();
        this._skillTypeWindow.activate();
    }

    playSeForItem() {
        SoundManager.playUseSkill();
    }

    determineItem(item){
        if(!this._action){
            this._action=new Game_Action();
        }else{
            this._action.clear();
        }
        let action=this._action;
        let actor=this.actor();
        action.setSubject(actor);
        action.setItemObject(item);
        if (action.isForFriend()) {
            if(action.isForOne()){
                this._actorWindow.open();
                this._actorWindow.activate();
                this._itemWindow.deactivate();
            }else if(action.isForUser()){
                this.useItem(actor,action,item);
            }else if(action.isForAll()&&$gameParty.canUse(item)){
                this._useForAll(action,actor);
            }
        } else {
            this.useItem(actor,action,item);
        }
    }

    useItem(actor,action,item){
        if(actor.canUse(item)){
             this.playSeForItem();
            actor.useItem(item);
            let times=action.numRepeats();
            for(let i=0;i<times;++i){
                action.apply(actor);
            }
            action.applyGlobal();

            this._statusWindow.refresh();
        }
    }

    _useForAll(action,actor){
        action.setSubject(actor);
        actor.useItem(action.item());
        let times=action.numRepeats();
        for(let battler of $gameParty.members()){
            for(let i=0;i<times;++i){
                action.apply(battler);
            }
        }
        action.applyGlobal();
        // this.checkCommonEvent();
        // this.checkGameover();
    }

}